﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;
using Sector7.Framework.Core;
using Sector7.Framework.DataDictionary.Agents;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// DataTemplateItemSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataTemplateItemSummaryWindow : AddModeWindowBase
    {
        #region Accessor
        public List<S7_DataTemplateItem> SelectedItems
        {
            get { return m_entity as List<S7_DataTemplateItem>; }
            set { m_entity = value; }
        }

        public S7_DataTemplate DataTemplates
        {
            get { return m_entity as S7_DataTemplate; }
            set { m_entity = value; }
        }
        #endregion

        #region Structure
        public DataTemplateItemSummaryWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region Private Functions
        /// <summary>
        /// 页面加载数据
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void DataTemplateItemSummaryWindow_Loaded(object obj, RoutedEventArgs e)
        {
            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new System.ComponentModel.DoWorkEventHandler(LoadData_DoWork);
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 加载数据工作线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadData_DoWork(object obj, System.ComponentModel.DoWorkEventArgs e)
        {
            GetData();
            this.Dispatcher.BeginInvoke(new DelegateWithNothing(LoadData));
        }

        /// <summary>
        /// 获取所有数据模板子项未选项
        /// </summary>
        protected override void GetData()
        {
            m_unselectItems = DataTemplateServiceAgent.GetAllMetadatas();
        }

        /// <summary>
        /// 加载所有数据模板子项未选项
        /// </summary>
        private void LoadData()
        {
            ClientUtil.LoadItemsSource(dtgUnselectItem, m_unselectItems);
            //ClientUtil.LoadItemsSource(dtgSelectedItem, m_selectedItems);
        }

        protected override void DoNew()
        {
            base.DoNew();

            AddMetadataWindow addWindow = new AddMetadataWindow();
            addWindow.InitializeWindow(CrudType.Create);
            addWindow.AddSuccess += new EventHandler(addWindow_AddSuccess);

            addWindow.ShowDialog();
        }

        void addWindow_AddSuccess(object sender, EventArgs e)
        {
            //将子页面AddMetadataWindow的值封装到MetaData,然后填充其父页面
            var addItemWindow = sender as AddMetadataWindow;
            var newMetadata = addItemWindow.Metadata;

            m_unselectItems.Add(newMetadata);

            ClientUtil.LoadSelectedItem(dtgUnselectItem, m_unselectItems);
        }

        private void dtgUnselectItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var addItemWindow = new AddMetadataWindow();
            addItemWindow.InitializeWindow(CrudType.Create);
            addItemWindow.AddSuccess += new EventHandler(addItemWindow_AddSuccess);

            addItemWindow.ShowDialog();
        }

        void addItemWindow_AddSuccess(object sender, EventArgs e)
        {
            var addItemWindow = sender as AddMetadataWindow;
            var newMetadata = addItemWindow.Metadata;

            m_unselectItems.Add(newMetadata);

            ClientUtil.LoadSelectedItem(dtgUnselectItem, newMetadata);

            addItemWindow.ResetWindow();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            foreach (S7_Metadata metadata in dtgUnselectItem.SelectedItems)
            {
                m_selectedItems.Add(metadata);
                m_unselectItems.Remove(metadata);
            }

            ClientUtil.LoadItemsSource(dtgUnselectItem, m_unselectItems);
            ClientUtil.LoadItemsSource(dtgSelectedItem, m_selectedItems);
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            foreach (S7_Metadata metadata in dtgSelectedItem.SelectedItems)
            {
                m_unselectItems.Add(metadata);
                m_selectedItems.Remove(metadata);
            }

            ClientUtil.LoadItemsSource(dtgUnselectItem, m_unselectItems);
            ClientUtil.LoadItemsSource(dtgSelectedItem, m_selectedItems);
        }

        protected override bool CheckData()
        {
            m_errors.Clear();

            int count = dtgSelectedItem.Items.Count;

            if (count <= 0)
            {
                m_errors.Add("已选数据元列表", "不能为空!");
            }

            return base.CheckData();
        }

        protected override void AddData()
        {
            var metadatas = new List<S7_DataTemplateItem>();

            foreach (var metadata in m_selectedItems)
            {
                S7_DataTemplateItem selectedItem = new S7_DataTemplateItem();
                selectedItem.MetadataId = metadata.MetadataId;
                selectedItem.Metadata = metadata;

                metadatas.Add(selectedItem);
            }

            SelectedItems = metadatas;

            base.AddData();
        }

        private void btnAddMetadata_Click(object sender, RoutedEventArgs e)
        {
            DoNew();
        }
        #endregion

        #region Inner members
        /// <summary>
        /// 获取所有数据模板子项未选项
        /// </summary>
        private List<S7_Metadata> m_unselectItems = null;

        /// <summary>
        /// 获取所有数据模板子项已选项
        /// </summary>
        private List<S7_Metadata> m_selectedItems = new List<S7_Metadata>();
        #endregion
    }
}
